package com.qj.yantaishan.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.qj.yantaishan.common.utils.TimeMyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.qj.yantaishan.mapper.StoreMapper;
import com.qj.yantaishan.domain.Store;
import com.qj.yantaishan.service.IStoreService;

/**
 * 店铺Service业务层处理
 *
 * @author qihe
 * @date 2022-07-22
 */
@Service
@Primary
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
    @Autowired
    private StoreMapper storeMapper;

    /**
     * 查询店铺
     *
     * @param storeId 店铺主键
     * @return 店铺
     */
    @Override
    public Store selectStoreByStoreId(Integer storeId) {
        return storeMapper.selectStoreByStoreId(storeId);
    }

    /**
     * 查询店铺列表
     *
     * @param store 店铺
     * @return 店铺
     */
    @Override
    public List<Store> selectStoreList(Store store) {
        return storeMapper.selectStoreList(store);
    }

    /**
     * 新增店铺
     *
     * @param store 店铺
     * @return 结果
     */
    @Override
    public int insertStore(Store store) {
        return storeMapper.insertStore(store);
    }

    /**
     * 修改店铺
     *
     * @param store 店铺
     * @return 结果
     */
    @Override
    public int updateStore(Store store) {
        return storeMapper.updateStore(store);
    }

    /**
     * 批量删除店铺
     *
     * @param storeIds 需要删除的店铺主键
     * @return 结果
     */
    @Override
    public int deleteStoreByStoreIds(String storeIds) {
        return 0;
    }

    /**
     * 删除店铺信息
     *
     * @param storeId 店铺主键
     * @return 结果
     */
    @Override
    public int deleteStoreByStoreId(Integer storeId) {
        return 0;
    }

    /**
     * 获取店铺概况
     * 1.店铺信息(店铺名、店铺logo)
     * select name,logo from shanglala_store
     * <p>
     * 2.昨日新增会员 select user  where   reg_time
     * select count(*) from shanglala_user where reg_time >#{startTime} and reg_time <#{endTime}
     * <p>
     * 3.昨日订单数 select order where  add_time
     * select count(*) from shanglala_order where add_time  >1659456000 and add_time <1659542399
     * 4.昨日收益 select  order where add_time and status
     * 5.待处理订单 select order where status in 2,6
     * select count(*) from shanglala_order where status in (2,6)
     * 6.商品 select product
     * select count(*) from shanglala_product where store_id = 1 and status =1 and soldout=0 and  quantity >0
     * <p>
     * 流量趋势
     * 店铺浏览次数:当日店铺浏览次数
     * select count(*) from shanglala_store_analytics  where store_id =1 and visited_time  >1659456000 and visited_time <1659542399
     * 会员数：当日新增会员数
     * select count(*) from shanglala_user where reg_time >#{startTime} and reg_time <#{endTime}
     * 订单退款额：当日订单退款金额
     * select sum(srp.pro_price*srp.pro_num) from shanglala_return  sr inner join  shanglala_return_product  srp on sr.id = srp.return_id  where dateline>1659545999 and dateline<1659542399 and  sr.status=3 or sr.status=5
     * <p>
     * 订单交易额：当日已支付订单金额
     * select sum(pay_money) from shanglala_order  where paid_time>1659545999 and paid_time<1659607218
     * <p>
     * <p>
     * 商品销量统计(前十）
     * select product_id ,count(product_id) from shanglala_order_product   where order_id in (select order_id from shanglala_order where add_time >1659283200 and add_time<1659604078 ) GROUP BY product_id
     * <p>
     * 购买率  每月所有用户中有过交易行为的用户比例
     * 查出那个月时的会员数量  user add_time < 月份时间挫   查出那个月时交易数量  add_time >月份开始  < 月份结束
     */
    @Override
    public Map<String, Object> getStore() {
        Map<String, Object> map = new HashMap<>();

        /**
         * 店铺信息
         */
        CompletableFuture<Void> storeMap1 = CompletableFuture.runAsync(() -> {
            Map<String, String> storeMap = storeMapper.getStoreMap();
            map.put("storeMap", storeMap);
        });


        /**
         * 2.昨日新增会员 select user  where   reg_time
         * select count(*) from shanglala_user where reg_time >#{startTime} and reg_time <#{endTime}
         */
        Map<String, Long> startAndEndTime = TimeMyUtil.getStartAndEndTime(new Date().getTime() / 1000, -1);

        CompletableFuture<Void> regNumberFuture = CompletableFuture.runAsync(() -> {
            int regNumber = storeMapper.selectYesterdayRegMember(startAndEndTime.get("startTime"), startAndEndTime.get("endTime"));
            map.put("regNumber", regNumber);
        });
        /**
         * 3.昨日订单数 select order where  add_time
         * select count(*) from shanglala_order where add_time  >1659456000 and add_time <1659542399
         */
        CompletableFuture<Void> orderNumberFuture = CompletableFuture.runAsync(() -> {
            int orderNumber = storeMapper.selectYesterdayOrderMember(startAndEndTime.get("startTime"), startAndEndTime.get("endTime"));
            map.put("orderNumber", orderNumber);

        });
        /**
         * 5.待处理订单 select order where status in 2,6
         *  select count(*) from shanglala_order where status in (2,6)
         */
        CompletableFuture<Void> waitOrderNumber1 = CompletableFuture.runAsync(() -> {
            int waitOrderNumber = storeMapper.selectWaitOrderNumber();
            map.put("waitOrderNumber", waitOrderNumber);
        });
        /**
         * 6.商品 select product
         *  select count(*) from shanglala_product where store_id = 1 and status =1 and soldout=0 and  quantity >0
         */
        CompletableFuture<Void> productNumber1 = CompletableFuture.runAsync(() -> {
            int productNumber = storeMapper.selectProductNumber();
            map.put("productNumber", productNumber);
        });

        /**
         * * 流量趋势  7天
         *      * 店铺浏览次数:当日店铺浏览次数
         *      * select count(*) from shanglala_store_analytics  where store_id =1 and visited_time  >1659456000 and visited_time <1659542399
         *      * 会员数：当日新增会员数
         *      * select count(*) from shanglala_user where reg_time >#{startTime} and reg_time <#{endTime}
         *      * 订单退款额：当日订单退款金额
         *      * select sum(srp.pro_price*srp.pro_num) from shanglala_return  sr inner join  shanglala_return_product  srp on sr.id = srp.return_id  where dateline>1659545999 and dateline<1659542399 and  sr.status=3 or sr.status=5
         *      * <p>
         *      * 订单交易额：当日已支付订单金额
         *      * select sum(pay_money) from shanglala_order  where paid_time>1659545999 and paid_time<1659607218
         */
        final CompletableFuture<Void> flowFuture = CompletableFuture.runAsync(() -> {
            List<Map<String, Object>> flowMapList = new ArrayList<>();
            for (int i = 0; i > -7; i--) {
                Map<String, Object> flowMap = new HashMap<>();
                Map<String, Long> flowStartAndEndTime = TimeMyUtil.getStartAndEndTime(new Date().getTime() / 1000, i);
                final Long startTime = flowStartAndEndTime.get("startTime");
                final Long endTime = flowStartAndEndTime.get("endTime");
                int nowDayFlowNumber = storeMapper.selectNowDayFlowNumber(startTime, endTime);
                int nowDayNewMember = storeMapper.selectNowDayNewMemberNumber(startTime, endTime);
                int nowDayReturnOrderNumber = storeMapper.selectNowDayReturnOrderNumber(startTime, endTime);
                BigDecimal nowDayPayMoney = storeMapper.selectNowDayPayMoney(startTime, endTime);
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                final String format = dateFormat.format(new Date(startTime * 1000));
                flowMap.put("date", format);    //日期
                flowMap.put("nowDayFlowNumber", nowDayFlowNumber);   //当日浏览量
                flowMap.put("nowDayNewMember", nowDayNewMember); //当日新增会员
                flowMap.put("nowDayReturnOrderNumber", nowDayReturnOrderNumber); //当日退款订单
                flowMap.put("nowDayPayMoney", nowDayPayMoney);  //当日支付金额
                flowMapList.add(flowMap);
            }
            map.put("flowMapList", flowMapList); //流量趋势
        });

        /**
         * * 商品销量统计(前十）
         * select sp.name ,count(sop.product_id) from shanglala_order_product sop  inner join shanglala_product sp
         * on sop.product_id= sp.product_id where order_id in
         * (select order_id from shanglala_order where add_time >1659283200 and add_time<1659604078 )  GROUP BY sop.product_id limit 0,10
         */
        Map<String, Object> sale10Product = new HashMap<>();
        //昨日
        Map<String, Long> yesterday = TimeMyUtil.getStartAndEndTime(new Date().getTime() / 1000, -1);

        List<Map<String, Object>> yesterdaySaleProductList = storeMapper.selectYesterdaySaleProduct(yesterday.get("startTime"), yesterday.get("endTime"));
        sale10Product.put("yesterdaySaleProductList", yesterdaySaleProductList);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {


        });
        //当天时间
        CompletableFuture<Void> weekSaleFuture = CompletableFuture.runAsync(() -> {


        });
        //近一周 week
        Map<String, Long> week = TimeMyUtil.getStartAndEndTime(new Date().getTime() / 1000, -7);
        List<Map<String, Object>> weekSaleProductList = storeMapper.selectWeekSaleProduct(week.get("startTime"), new Date().getTime() / 1000);
        sale10Product.put("weekSaleProductList", weekSaleProductList);
        //近一个月
        final CompletableFuture<Void> mountSaleFuture = CompletableFuture.runAsync(() -> {

        });
        Map<String, Long> mouth = TimeMyUtil.getStartAndEndTime(new Date().getTime() / 1000, -30);
        List<Map<String, Object>> mountSaleProductList = storeMapper.selectMountSaleProduct(mouth.get("startTime"), new Date().getTime() / 1000);
        sale10Product.put("mountSaleProductList", mountSaleProductList);

        CompletableFuture.allOf(storeMap1, regNumberFuture, orderNumberFuture, waitOrderNumber1, productNumber1, flowFuture
                , voidCompletableFuture, weekSaleFuture, mountSaleFuture).join();
        map.put("sale10Product", sale10Product);
        /**
         * 每月购买率
         */
        List<Map<String, Object>> pay = new ArrayList<>();
        for (int i = 0; i > -12; i--) {

            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Map<String, Long> longMap = TimeMyUtil.caleMount(dateFormat.format(date), i);

            double payMount = storeMapper.getPayMount(longMap.get("startTime"), longMap.get("endTime"));
            final Long endTime = longMap.get("endTime");
            final Long startTime = longMap.get("startTime");
            final Date date1 = new Date(startTime * 1000);
            SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM");

            Map<String, Object> map2 = new HashMap<>();
            map2.put("payMount", payMount);   //购买率
            map2.put("date", dateFormat2.format(date1));//日期
            pay.add(map2);
        }
        map.put("pay", pay);   //购买率
        return map;
    }
}
